package org.example.service.imp;

import org.example.dao.DiscussPostDao;
import org.example.domain.DiscussPost;
import org.example.domain.User;
import org.example.service.DiscussService;
import org.example.util.HutoolUtil;
import org.example.vo.IndexPageVO;
import org.example.vo.LimitPageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DiscussServiceImp implements DiscussService {
    private static Logger logger = LoggerFactory.getLogger(DiscussServiceImp.class);
    @Autowired
    private DiscussPostDao discussPostDao;
    @Autowired
    private UserServiceImp userServiceImp;
    @Autowired
    private LikeServiceImp likeServiceImp;
    @Autowired
    private FollowServiceImp followServiceImp;
    @Autowired
    private CommentServiceImp commentServiceImp;
    @Autowired
    private HutoolUtil hutoolUtil;
    @Override
    public int importAll(List<DiscussPost> discussPostList) {
        return discussPostDao.importAll(discussPostList);
    }

    @Override
    public List<DiscussPost> searchByLimitParam(int page, int limit) {
        //需要做参数处理 page可能出现异常
        return discussPostDao.searchByLimitParam(page,limit);
    }

    @Override
    public IndexPageVO dynamicSearchByUserId(Integer userId, Integer page, Integer limit) {
        IndexPageVO result = new IndexPageVO();
        if (page<1) page = 1;
        //存在userId就按照条件查询 反之  处理page上限
        int count = searchCount(userId);
        //一共可以分多少页
        int remainder = count%limit;
        int totalLimitCount = remainder!=0?(count/limit)+1:count/limit;
        if(page>totalLimitCount) page = totalLimitCount;

        List<DiscussPost> discussPosts = discussPostDao.dynamicSearchByUserId(userId, (page-1)*limit, limit);
        result.setDiscussPost(discussPosts);

        List<User> users = new ArrayList<>();
        for (int i = 0;i<discussPosts.size();i++){
            //通过userId获取user    这里一个个获取性能很慢
            User user = userServiceImp.searchById(discussPosts.get(i).getUserId());
            users.add(user);
        }
        result.setRelationUser(users);
        LimitPageVO limitPageVO = new LimitPageVO();
        limitPageVO.setCurrent(page);
        limitPageVO.setRows(discussPosts.size());
        limitPageVO.setTotal(totalLimitCount);
        limitPageVO.setFrom(page);
        limitPageVO.setTo(totalLimitCount);

        result.setLimitPageVO(limitPageVO);
        return result;
    }

    /**
     * 这个和 上面那个方法很像
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IndexPageVO searchHotDiscuss(int page, int limit) {
        if (page<0 || limit<=0) return null;
        IndexPageVO result = new IndexPageVO();
        int totalCount = discussPostDao.searchCount(null);
        LimitPageVO limitPageVO = hutoolUtil.limitPage(page, limit, totalCount);
        result.setLimitPageVO(limitPageVO);
        List<DiscussPost> discussPosts = discussPostDao.searchHotDiscuss((limitPageVO.getCurrent()-1)*limit, limit);
        result.setDiscussPost(discussPosts);
        List<User> users = new ArrayList<>();
        for (int i = 0;i<discussPosts.size();i++){
            //通过userId获取user    这里一个个获取性能很慢
            User user = userServiceImp.searchById(discussPosts.get(i).getUserId());
            users.add(user);
        }
        result.setRelationUser(users);


        return result;
    }

    @Override
    public int searchCount(Integer userId) {
        return discussPostDao.searchCount(userId);
    }

    /**
     *
     * @param page
     * @param limit
     * @param userId  用来表示分页查询参数  null通查
     */
    public void handlerLimitParam(int page,int limit,Integer userId){
        //page为前端需要展示的页码  limit 一页展示多少个
        if (page<1) page = 1;
        //数据能否分到这么多页
        int totalCount = searchCount(userId);

    }

    @Override
    public int saveOne(DiscussPost discussPost) {
        if (discussPost==null) return 0;
        return discussPostDao.saveOne(discussPost);
    }

    @Override
    public DiscussPost searchById(int id) {
        if (id<=0) return null;
        return discussPostDao.searchById(id);
    }

    @Override
    public List<Integer> searchUserDiscussIds(int userId) {
        if (userId<=0) return null;
        return discussPostDao.searchUserDiscussIds(userId);
    }

    @Override
    public int updateType(int id, int type) {
        if (id <=0 || type<0) return 0;
        int i = discussPostDao.updateType(id, type);
        return i;
    }

    @Override
    public int updateStatus(int id, int status) {
        if (id <=0 || status<0) return 0;
        int i = discussPostDao.updateStatus(id,status);
        return i;
    }


    public Integer computeScore(int discussId){
        if (discussId<=0) return 0;
        //需要计算当前帖子的分数  这个分数 受到了 帖子的点赞 关注 评论数量的影响 需要先获取这个数量 然后做统计
        int likeCount = likeServiceImp.getLikeCount(1,discussId);
        //我都不知道我前面写了一些什么东西 算啦 帖子的关注和评论都不计算了  这里主要是想测试quartz的使用
        //我就用点赞数量作为分数 简单意思一下
        return likeCount;

    }

    @Override
    public int updateScoreById(Integer id, Double score) {
        if (id <=0) return 0;
        try {
            discussPostDao.updateScoreById(id, score);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return 1;
    }
}
